package com.syx.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.objects.XObject;
import com.syx.entity.Article;
import com.syx.service.ArtService;
import com.syx.util.FileUploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ArtController {
    @Resource
    private ArtService artService;

    //列表
    @RequestMapping("list.do")
    public String artList(Model model,
                          String title,
                          String content,
                          String names,
                          @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum){
        PageHelper.startPage(pageNum,3);
        Map map = new HashMap();
        map.put("title",title);
        map.put("content",content);
        map.put("names",names);
        List list = artService.artList(map);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("map",map);
        return "list";
    }

    //添加中转
    @RequestMapping("addTo.do")
    public String addTo(){
        return "add";
    }

    //修改中转
    @RequestMapping("updTo.do")
    public String updTo(Integer id){
        return "upd";
    }

    //下拉列表
    @RequestMapping("tagList.do")
    @ResponseBody
    public Object tagList(){
        List list = artService.tagList();
        System.out.println(list);
        return list;
    }

    //添加
    @RequestMapping("add.do")
    @ResponseBody
    public int artAdd(Article article, HttpSession session, MultipartFile photo, Integer[] tagids){
        if (photo.getSize()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            article.setPic(upload);
        }
        int  i = artService.add(article,tagids);
        return 1;
    }

    //删除
    @RequestMapping("del.do")
    @ResponseBody
    public int del(Integer id){
        int  i = artService.del(id);
        return 1;
    }

    //回显
    @RequestMapping("hx.do")
    @ResponseBody
    public Object hx(Integer id){
        Map map  =artService.hx(id);
        return map;
    }
}
